// 欢迎来到c代码的世界
#include "types.h"
#include "string.h"

extern void init_sys_mmu(void);
extern void start_mmu(void);
extern void printk(char *fmt, ...);
extern void memory_test(void);
extern void RamDiskTest(void);
extern void vfs_test(void);
extern void excute_test_bin(void);
extern void excute_test_elf(void);
extern void timer_init(void);
extern int task_init(void);
extern int do_fork(int (*f)(void *), void *args);

#define USART_PHYSICAL_ADDR 0x50000020
static void helloworld(void)
{
	const char *p = "helloworld\n";
	while (*p)
	{
		*(volatile unsigned int *)USART_PHYSICAL_ADDR = *p;
		p++;
	};
}

void delay(void)
{
	volatile unsigned int time = 0xffff;
	while (time--)
	{
		;
	}
}

int test_process(void *p)
{
	while (1)
	{
		delay();
		printk("test process %dth\n", (int)p);
	}
	return 0;
}

void boot(void)
{
	helloworld();
	init_sys_mmu();
	start_mmu();
	// memory_test();
	// RamDiskTest();
	// vfs_test();
	// excute_test_bin();
	// excute_test_elf();

	task_init();
	timer_init();
	if (do_fork(test_process, (void *)1) == -1)
	{
		printk("create task 1 error\n");
	}
	if (do_fork(test_process, (void *)2) == -1)
	{
		printk("create task 2 error\n");
	}

	while (1)
	{
		printk("this is main task\n");
		delay();
	}

	printk("in boot\n");
}

void handle_abnormal_undefined(void)
{
	printk("handle_abnormal_undefined\n");
	for (;;)
	{
		;
	}
}

void handle_abnormal_swi(void)
{
	printk("handle_abnormal_swi\n");
	for (;;)
	{
		;
	}
}

void handle_abnormal_prefetch(void)
{
	printk("handle_abnormal_prefetch\n");
	for (;;)
	{
		;
	}
}

void handle_abnormal_data(void)
{
	printk("handle_abnormal_data\n");
	for (;;)
	{
		;
	}
}

void handle_abnormal_reserved(void)
{
	printk("handle_abnormal_reserved\n");
	for (;;)
	{
		;
	}
}